package com.skivingcloud.admin.sys.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.skivingcloud.admin.annotation.UserOperateLog;
import com.skivingcloud.admin.sys.entity.SysDictionary;
import com.skivingcloud.admin.sys.service.ISysDictionaryService;
import com.skivingcloud.common.utils.PageUtils;
import com.skivingcloud.common.utils.R;
import com.skivingcloud.common.utils.StringUtils;
import com.skivingcloud.common.utils.TreeUtils;
import com.skivingcloud.common.utils.UuidUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;

/**
 * <p>
 *  数据字典前端控制器
 * </p>
 *
 * @author skivingcloud
 * @since 2022-01-29
 */
@RestController
@Api(tags = "数据字典管理")
@RequestMapping("/sys/dic")
public class SysDictionaryController {

	private static Logger log = LoggerFactory.getLogger(SysDictionaryController.class);

	@Autowired
	private ISysDictionaryService sysDictionaryService;

	/**
	 * 所有配置列表
	 */
	@PostMapping("/list")
	public R list(@RequestBody Map<String, Object> params) {
		/*Map<String, Object> params = new HashMap<String, Object>();
		params.put("page", page);
		params.put("limit", limit);
		params.put("dataForm", dataForm);*/
		PageUtils pages = sysDictionaryService.queryPage(params);
		return R.ok().put("page", pages);
	}

	/**
	 * 根据字典编号取字典数据
	 * 
	 * @param classCode
	 * @return
	 */
	@GetMapping("/getDiclist/{classCode}")
	public R getDiclist(@PathVariable("classCode") String classCode) {
		List<SysDictionary> list = sysDictionaryService.queryByClassCode(classCode);
		return R.ok().put("list", list);
	}
	
	/**
	 * 配置信息
	 * 
	 * @throws Exception
	 */
	@GetMapping("/info/{id}")
	public R info(@PathVariable("id") String id) throws Exception {
		SysDictionary dictionary = sysDictionaryService.selectById(id);
		return R.ok().put("dic", dictionary);
	}

	/**
	 * 保存配置
	 */
	@UserOperateLog(module = "系统功能", methods = "save", description = "新增数据字典")
	@PostMapping("/save")
	public R save(@RequestBody SysDictionary dictionary) {
		try {
			dictionary.setId(UuidUtil.get32UUID());
			if (StringUtils.isBlank(dictionary.getParentId())) {
				dictionary.setParentId("0");
			}
			sysDictionaryService.saveNew(dictionary);
			return R.ok();
		} catch (Exception e) {
			log.error("保存数据字典失败!", e);
			return R.error("保存数据字典失败!");
		}
	}

	/**
	 * 修改配置
	 */
	@UserOperateLog(module = "数据字典管理", methods = "update", description = "修改数据字典")
	@PostMapping("/update")
	public R update(@RequestBody SysDictionary dictionary) {
		try {
			if (StringUtils.isBlank(dictionary.getParentId())) {
				dictionary.setParentId("0");
			}
			sysDictionaryService.update(dictionary);
			return R.ok();
		} catch (Exception e) {
			log.error("保存数据字典失败!", e);
			return R.error("保存数据字典失败!");
		}
	}

	/**
	 * 删除配置
	 */
	@UserOperateLog(module = "系统功能", methods = "delete", description = "删除数据字典")
	@PostMapping("/delete")
	public R delete(@RequestBody String[] ids) {
		try {
			sysDictionaryService.deleteBatch(ids);
			return R.ok();
		} catch (Exception e) {
			log.error("删除数据字典失败!", e);
			return R.error("删除数据字典失败!");
		}
	}

	/**
	 * 
	 * <p>
	 * Title: 根据字典代码加载数据字典树结构
	 * </p>
	 * <p>
	 * Description: 根据字典代码加载数据字典树结构
	 * </p>
	 * 
	 * @param classCode 字典代码,多个用逗号隔开
	 * @return
	 */
	@GetMapping("/getDicTreeByClassCode/{classCode}")
	public R getDicTreeByClassCode(@PathVariable("classCode") String classCode) {
		try {
			List<TreeUtils> list = sysDictionaryService.getDicTreeByClassCode(classCode);
			return R.ok().put("dicTree", list);
		} catch (Exception e) {
			log.error("根据字典代码加载数据字典树结构失败!", e);
			e.printStackTrace();
			return R.error(1, "根据字典代码加载数据字典树结构失败!");
		}
	}

	/**
	 * 根据字典编码获取字典数据
	 * 
	 * @param classCode
	 * @return [
	 *         {
	 *         "label": "check 1",
	 *         "value": 1
	 *         },
	 *         {
	 *         "label": "check 2",
	 *         "value": 2
	 *         },
	 *         {
	 *         "label": "check 3",
	 *         "value": 3
	 *         }
	 *         ]
	 */
	@GetMapping("/getDicMapByclassCode/{classCode}")
	public List<Map<String, Object>> getDicMapByclassCode(@PathVariable("classCode") String classCode) {
		return sysDictionaryService.getDicMapByclassCode(classCode);
	}
}
